/*  ----------------------------------------------------------------------------
*	This file is part of the AS3This ActionScript 3 library.
*	Author: Steve Jones for G-RAFF <http://www.g-raff.com/>
*	Copyright (c) 2009 raffWorks Ltd.
*	----------------------------------------------------------------------------
*	Please see the notice at the bottom of the code before using or modifying this class.
*/
package com.as3this
{
    import com.as3this.core.Destination;
    import com.as3this.core.ShareContent;
    import com.as3this.interfaces.IShareContent;
    
    import flash.events.EventDispatcher;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.navigateToURL;
   
   /**
	 * AddThisApi is the core class of the API
	 * 
	 * @author Steve Jones
	 */
	 
    public class AddThisApi extends EventDispatcher
    {
        private var _username	:String;
        private var _mode		:String;
        
        /**
		 * Constructor
		 *  
		 * @param username	AddThis username - options
		 * @param mode		Mode by which to share content
		 * <p>Content can be shared using two modes, specified as constants in the <code>ShareMode</code> class:</p>
		 * <p><code>VIA_META_TAGS</code>: This is AddThis's recommended route, but you will need to set your
		 * receiving page to dynamically update the meta tags. This is easily done with PHP and an example is 
		 * included with the AS3This distribution
		 */
		 
        public function AddThisApi(username:String="", mode:String = "shareMode/viaApi")
        {
            _username = username;
            _mode = mode;
        }
        
        /**
		 * <p>This is the core method of the API. It takes a content object and a destination, and calls the URL
		 * with all variables correctly wrapped and formatted for sharing.</p>
		 * 
		 * @param	content		A content object that implements the IShareContent interface.
		 * @param	destination	The chosen service to which the content will be shared
		 * <p>The <code>Destination</code> class has a full set of constants to determine available services<o>
		 *	
		 * @see com.as3this.interfaces.IShareContent
		 * @see com.as3this.core.Destination
		 * 
		 * @example The following code simply shares a link to Twitter:
		 * <listing version="3.0">
		 * // create the content object
		 * var content:IShareContent = new ShareContent("http://demo.as3this.com/", ShareContent.LINK);
		 * 
		 * // instantiate an API object with the ShareMode.VIA_API method (could use ShareMode.VIA_META_TAGS but this
		 * // requires reference to an external PHP file and is a bit beyond these docs)
		 * var api:AddThisApi = new AddThisApi(ShareMode.VIA_API);
		 * 
		 * // and share!
		 * api.share(content, Destination.TWITTER);
		 * </listing>
		 * 
		 * @example The following code shares an embedded SWF with some dynamic variables to Facebook
		 * <listing version="3.0">
		 * 
		 * // create the options object first
		 * var options:IShareOptions = new RichShareOptions("Content Title", "Some description", "http://demo.as3this.com/demo.swf", 320, 260, "http://demo.as3this.com/demo.jpg");
		 * options.setFlashVar("test_variable", "This works");
		 *  
		 * // create the content object, passing this options object into the constructor
		 * var content:IShareContent = new ShareContent("http://demo.as3this.com/", ShareContent.FLASH, options);
		 * 
		 * // instantiate an API object with the ShareMode.VIA_API method 
		 * var api:AddThisApi = new AddThisApi(ShareMode.VIA_API);
		 * 
		 * // and share!
		 * api.share(content, Destination.FACEBOOK);
		 * </listing>
		 */
		 
        public function share(content:IShareContent, destination:String = Destination.MENU) : void
        {
            // create a URLVariables class to hold the sharing vars
            var urlVars:URLVariables = content.getVariables(_mode);
           
            // username
            if (_username) urlVars.username = _username;
          	
            var request:URLRequest = new URLRequest(Destination.getEndpoint(destination));
            request.data = urlVars;
            navigateToURL(request, "_blank");
        }
        
        /**
		 * Set the share mode of the API
		 * @param	mode		Preferred mode of sharing the content
		 * <p>Can be either <code>ShareMode.VIA_META_TAGS</code> (preferred by AddThis) or <code>ShareMode.VIA_API</code><p>
		 *	
		 * @see ShareMode
		 */
		 
        public function set shareMode(mode:String) : void
        {
        	_mode = mode;
        }
        
        public function get shareMode() : String
        {
        	return _mode;
        }
    }
}
/*
 *    AS3This is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    AS3This is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with AS3This.  If not, see <http://www.gnu.org/licenses/>.
 */